import os
import random
import time
from string import Template

import cv2 as cv
# from skimage.metrics import structural_similarity


def screenshot(path=None):
    """
    截图
    :param path:
    :return:
    """
    if path is None:
        file_name = "current.png"
    else:
        file_name = str(time.time()).replace('.', '') + ".png"
    path = "current_img/" + file_name
    command_temp = Template('adb exec-out screencap -p > ${path}')
    command = command_temp.substitute(path=path)
    os.system(command)
    img = cv.imread(path)
    return img


def go_back():
    """
    模拟后退
    :return:
    """
    command = "adb shell input keyevent BACK"
    os.system(command)
    time.sleep(0.1)


def swipe(x1, y1, x2, y2):
    """
    模拟滑动
    :param x1:
    :param y1:
    :param x2:
    :param y2:
    :return:
    """
    command_temp = Template("adb shell input swipe ${x1} ${y1} ${x2} ${y2} 100")
    command = command_temp.substitute(x1=x1, y1=y1, x2=x2, y2=y2)
    os.system(command)
    time.sleep(0.1)


def tap(x, y):
    """
    模拟点击
    :param x:
    :param y:
    :return:
    """
    command_temp = Template("adb shell input tap ${x} ${y}")
    command = command_temp.substitute(x=x, y=y)
    os.system(command)


# def img_similar(img, t_img, position=None):
#     """
#     图片相似度计算
#     :param
#     img:
#     :param
#     t_img:
#     :param
#     position:
#     :return:
#     """
#     if position is not None:
#         img = img[position[1]:position[3], position[0]:position[2]]
#     img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
#     t_img = cv.cvtColor(t_img, cv.COLOR_BGR2GRAY)
#
#     (score, diff) = structural_similarity(img, t_img, full=True)
#     return score


def get_x_y(position):
    """
    获取随即坐标
    :param
    position:
    :return:
    """
    return random.randint(position[0], position[2]), random.randint(position[1], position[3])
